home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1062 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.2 KB  |  87 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: mutable can be removed...
  5. Date: 12 Apr 1996 11:07:19 PDT
  6. Organization: FakultΣt fⁿr Mathematik und Informatik
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4km58m$189@news.BelWue.DE>
  9. References: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>
  10. Reply-To: dietmar.kuehl@uni-konstanz.de
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 12 Apr 1996 17:52:22 GMT
  13. X-Newsreader: TIN [version 1.2 PL2]
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMW6b2Uy4NqrwXLNJAQHqGQH/aaT93Qq/K/pXxl5tDwa5VZjjWfpQWrY1
  16.     5Hz5DDWisMfVW1XWHficU6KtZPFbvs3mP0a16fD4QmIJgEEQvgnIdA==
  17.     =ZCDs
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. Hi,
  21. Igor Boukanov (Igor.Boukanov@fi.uib.no) wrote:
  22.  
  23. [example how to potentially simulate 'mutable' removed]
  24.  
  25. : And remove_constness_from is more useful than mutable because in this 
  26. : case to make any class member looks like mutable one don't even need to 
  27. : modify class definition! And of cause lines like 
  28. : "remove_constness_from(this)->i = i_;"
  29. : will show explicitly what somebody does...
  30.  
  31. This is why 'const_cast' was invented but it does not replace
  32. 'mutable'.  See dcl.type.cv section 4: "Except that any class member
  33. declared mutable can be modified,  any  attempt  to  modify  a  const
  34. object  during its lifetime results in undefined behavior."
  35.  
  36. Thus, the following would result in undefined behavior:
  37.  
  38.   class f
  39.   {
  40.     int count_;
  41.   public:
  42.     void count() const
  43.     { 
  44.       const_cast<f*>(this)->count_++;
  45.     }
  46.   };
  47.  
  48.   f const const_f;
  49.  
  50.   int main() { const_f.count(); return 0; }
  51.  
  52. If 'count_' is declare to be 'mutable', everything is fine. Now you
  53. could try to argue, that it would be necessary to remove decl.type.cv
  54. section 4 or rephrase it to make the above code well behaved. But this
  55. would prevent the ability to put 'const' objects into write-protected
  56. memory (like ROM) or to determine their value at compile-time.
  57.  
  58. Thus, 'mutable' is necessary and it has indeed some useful
  59. applications.  In particular, it allows to implement classes which
  60. maintain logical constness (instead of bitwise constness) in a
  61. convenient way. The fact that the bits of some 'const' object will be
  62. changed is already obvious from the (concious) declaration of the
  63. 'mutable' member.
  64.  
  65. : >From this point of view mutable keyword can be removed from C++. And it 
  66. : will not break any real code because as I now there is no compiler that 
  67. : can compile such code!
  68.  
  69. g++ implements 'mutable' and removing 'mutable' from the standard would
  70. break my code.  Whether you consider this code to be "real" code is a
  71. different question...
  72.  
  73. : And of cause this will make C++ standard shorter...
  74.  
  75. ... and less flexible.
  76. --
  77. dietmar.kuehl@uni-konstanz.de
  78. http://www.informatik.uni-konstanz.de/~kuehl/
  79. I am a realistic optimist - that's why I appear to be slightly pessimistic
  80. ---
  81. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  82.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  83.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  84.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  85.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  86. ]
  87.